package com.tulele.vtool.bus.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.mapper.UserInfoMapper;
import com.tulele.vtool.bus.model.DailyPaperInfo;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.thirdparty.WxNotifyMsgSetInfo;
import com.tulele.vtool.bus.service.IArticleService;
import com.tulele.vtool.bus.service.IUserBrandService;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.SysGzhService;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Component
@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private IUserBrandService userBrandService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private UserInfoMapper userInfoMapper;

//    @Value("${frontDomain}")
//    private String frontDomain="";
//
//    @Value("${openMemberMenuUrl}")
//    private String openMemberMenuUrl;

    @Autowired
    private SysGzhService sysGzhService;

    @Override
    public WxNotifyMsgSetInfo getUserWxMsgAcceptSwitchInfo(Integer userId) {

        Object wxUserMsgAcceptSwitchObj = redisCacheUtil.getCacheObject("wxUserMsgAcceptSwitch" + userId);

        WxNotifyMsgSetInfo wxNotifyMsgSetInfo = new WxNotifyMsgSetInfo();

        if (wxUserMsgAcceptSwitchObj != null) {

            String wxUserMsgAcceptSwitchStr = (String)wxUserMsgAcceptSwitchObj;

            log.info("[wxUserMsgAcceptSwitchStr] = " + userId + " " + wxUserMsgAcceptSwitchStr);

            wxNotifyMsgSetInfo = JSONObject.parseObject(wxUserMsgAcceptSwitchStr, WxNotifyMsgSetInfo.class);

        } else {

            wxNotifyMsgSetInfo.setNotifySwitchStatus(1);
            wxNotifyMsgSetInfo.setNightDisturbStatus(1);
            wxNotifyMsgSetInfo.setFirstTimeArticleAcceptStatus(1);
            wxNotifyMsgSetInfo.setSecondTimeArticleAcceptStatus(1);
            wxNotifyMsgSetInfo.setThirdTimeArticleAcceptStatus(1);
            wxNotifyMsgSetInfo.setForthTimeArticleAcceptStatus(1);
            wxNotifyMsgSetInfo.setFifthTimeArticleAcceptStatus(1);
            wxNotifyMsgSetInfo.setDailyDataReportAcceptStatus(1);
           // wxNotifyMsgSetInfo.setSixthTimeArticleAcceptStatus(1);

        }

        return wxNotifyMsgSetInfo;

    }

    @Override
    public Integer getNewUserBrandSetPushStatus(Integer userId) {

        //如果已经设置了品牌，则认为是已经推送
        int userSubBrandCount = Optional.ofNullable(userBrandService.getUserSubBrandCount(userId)).orElse(0);
        if (userSubBrandCount > 0) {
            return 1;
        }

        //如果redis中已经存在相关状态，则直接返回
        Object newUserBrandSetPushStatusObj = redisCacheUtil.getCacheObject("NewUserBrandSetPushStatus" + userId);
        if (newUserBrandSetPushStatusObj != null) {

            return (Integer)newUserBrandSetPushStatusObj;

        }

        //默认返回未推送
        return 0;
    }

    @Override
    public Integer getNewUserVisitPushStatus(Integer userId) {

        //如果redis中已经存在相关状态，则直接返回
        Object newUserVisitPushStatusObj = redisCacheUtil.getCacheObject("NewUserVisitPushStatus" + userId);
        if (newUserVisitPushStatusObj != null) {

            return (Integer)newUserVisitPushStatusObj;

        }

        //默认返回未推送
        return 0;
    }

    @Override
    public Integer getNewUserShareArticlePushStatus(Integer userId) {
        //如果已经分享了文章，则认为是已经推送
        int userShareArticleCount = Optional.ofNullable(articleService.getUserShareArticleCount(userId)).orElse(0);
        if (userShareArticleCount > 0) {

            return 1;

        }

        //如果redis中已经存在相关状态，则直接返回
        Object newUserShareArticlePushStatusObj = redisCacheUtil.getCacheObject("NewUserShareArticlePushStatus" + userId);
        if (newUserShareArticlePushStatusObj != null) {

            return (Integer)newUserShareArticlePushStatusObj;

        }

        //默认返回未推送
        return 0;
    }

    @Override
    public UserInfo getUserCreateTime(Integer userId) {
        return userInfoMapper.queryUserCreateTime(userId);
    }

    @Override
    public Integer getUserVipStatus(Integer userId) {
        return userInfoMapper.queryUserVipStatus(userId);
    }

    @Override
    public String generateVipDiscountExpirePushMsg(int couponRemainTime,String profile) {

        PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);

        String pushMsg = "\uD83C\uDF39\uD83C\uDF39\uD83C\uDF39亲，您的优惠卷将在"+ couponRemainTime +"小时过期哦。\n" +
                "\n" +
                "现在开通最高立省30元哦。\n" +
                "\n" +
                "<a href='"+ sysGzh.getOpenMemberMenuUrl() + "?mt=1'>点击开通会员</a>\n\n";

        return pushMsg;
    }

    @Override
    public UserInfo getUserSubPnInfo(Integer userId) {
        return userInfoMapper.queryUserSubPnInfo(userId);
    }

    @Override
    public Integer getLastestUserId() {
        return userInfoMapper.queryLastestUserId();
    }

    @Override
    public Integer getOldestUserId() {
        return userInfoMapper.queryOldestUserId();
    }

    @Override
    public UserInfo queryUserInfo(Integer userId) {
        return userInfoMapper.queryUserInfo(userId);
    }

    @Override
    public List<UserInfo> getUserListByGzhCode(String gzhCode){
        return userInfoMapper.getUserListByGzhCode(gzhCode);
    }

    @Override
    public List<UserInfo> getUserListByNoActivation(String gzhCode){
        return userInfoMapper.getUserListByNoActivation(gzhCode);
    }

    @Override
    public List<UserInfo> getUserByBrandId(String gzhCode,Integer brandId){
        return userInfoMapper.getUserByBrandId(gzhCode,brandId);
    }

    @Override
    public List<UserInfo> getAllUserListByGzhCode(String gzhCode){
        return userInfoMapper.getAllUserListByGzhCode(gzhCode);
    }

    @Override
    public List<UserInfo> getUserListByGzhCodeOut(String gzhCode){
        return userInfoMapper.getUserListByGzhCodeOut(gzhCode);
    }

    @Override
    public DailyPaperInfo getUserDailyPaper(Integer userId){
        return userInfoMapper.getUserDailyPaper(userId);
    }

    @Override
    public List<UserInfo> queryWillExpire(String profile) {
        return userInfoMapper.queryWillExpire(profile);
    }

    @Override
    public List<UserInfo> queryVIPHasExpired(String profile) {
        return userInfoMapper.queryVIPHasExpired(profile);
    }

    @Override
    public List<UserInfo> queryUserListByWeekly(String profile) {
        return userInfoMapper.queryUserListByWeekly(profile);
    }

    @Override
    public List<UserInfo> queryUserListByCommissionDeduction(String profile) {
        return userInfoMapper.queryUserListByCommissionDeduction(profile);
    }

    @Override
    public String getUserBrandName(Integer userId) {
        return userInfoMapper.getUserBrandName(userId);
    }

    @Override
    public List<UserInfo> queryNotVipUserList(String gzhCode) {
        return userInfoMapper.queryNotVipUserList(gzhCode);
    }

    @Override
    public List<UserInfo> queryNotVipNotDate(String gzhCode) {
        return userInfoMapper.queryNotVipNotDate(gzhCode);
    }

    @Override
    public String getSysGzh(String gzhCode) {
        return userInfoMapper.getSysGzh(gzhCode);
    }

    @Override
    public UserInfo getUserByOpenId(String openId,String gzhCode) {
        return userInfoMapper.getUserByOpenId(openId,gzhCode);
    }

    @Override
    public List<UserInfo> getUserByQqblQkb(){
        return userInfoMapper.getUserByQqblQkb();
    }
}
